# SPDX-FileCopyrightText: 2018-2025 Xavier Loux (BleuRaven)
#
# SPDX-License-Identifier: GPL-3.0-or-later

# ----------------------------------------------
#  Blender For UnrealEngine
#  https://github.com/xavier150/Blender-For-UnrealEngine-Addons
# ----------------------------------------------

import bpy
from typing import Any, Optional,Dict
from . import bfu_camera_data
from .. import bfu_export_text_files

def WriteOneFrameCameraAnimationTracks(obj: bpy.types.Object, target_frame: int, pre_bake_camera: Optional[bfu_camera_data.BFU_CameraTracks] = None):
    return WriteCameraAnimationTracks(obj, target_frame, target_frame+1, pre_bake_camera)

def WriteCameraAnimationTracks(obj: bpy.types.Object, target_frame_start: Optional[int] = None, target_frame_end: Optional[int] = None, pre_bake_camera: Optional[bfu_camera_data.BFU_CameraTracks] = None) -> Dict[str, Any]:
    # Write as data camera animation tracks

    if bpy.context is None:
        return {}
    scene = bpy.context.scene
    if target_frame_start is None:
        target_frame_start = scene.frame_start
    if target_frame_end is None:
        target_frame_end = scene.frame_end+1


    scene = bpy.context.scene
    data: Dict[str, Any] = {}
    bfu_export_text_files.bfu_export_text_files_utils.add_generated_json_header(data, bpy.app.translations.pgettext("This file contains additional Camera animation informations that is not supported with .fbx files", "interface.write_text_additional_track_camera"))
    bfu_export_text_files.bfu_export_text_files_utils.add_generated_json_meta_data(data)

    data["frame_start"] = target_frame_start
    data["frame_end"] = target_frame_end

 
    if pre_bake_camera:
        camera_tracks = pre_bake_camera.get_animated_values_as_dict()
    else:
        multi_camera_tracks = bfu_camera_data.BFU_MultiCameraTracks()
        multi_camera_tracks.add_camera_to_evaluate(obj)
        multi_camera_tracks.set_start_end_frames(target_frame_start, target_frame_end)
        multi_camera_tracks.evaluate_all_cameras(True)
        camera_tracks = multi_camera_tracks.get_evaluate_camera_data_as_dict(obj)

    data.update(camera_tracks)

    bfu_export_text_files.bfu_export_text_files_utils.add_generated_json_footer(data)
    return data

